import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/ui/pages/primitive/widgets/base_message_channel_page.dart';
import 'package:flutter_demo/ui/pages/primitive/widgets/event_channel_page.dart';
import 'package:flutter_demo/ui/pages/primitive/widgets/method_channel_page.dart';
import 'package:get/get.dart';

import 'index.dart';

//https://cloud.tencent.com/developer/article/1912165
class PrimitivePage extends GetView<PrimitiveController> {
  static const routeName = "/PrimitivePage";

  const PrimitivePage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.all(20),
          child: TextButton(
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                    const BorderSide(color: Colors.grey, width: 1)),
              ),
              onPressed: () {
                Navigator.push(
                    context,
                    CupertinoPageRoute<void>(
                        builder: (ctx) => BasicMessageChannelPage(
                            "BasicMessageChannel 实现 Flutter 与 原生双向通信")));
              },
              child: const Text(
                "BasicMessageChannel 通信",
              )),
        ),
        Padding(
          padding: const EdgeInsets.all(20),
          child: TextButton(
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                    const BorderSide(color: Colors.grey, width: 1)),
              ),
              onPressed: () {
                Navigator.push(
                    context,
                    CupertinoPageRoute<void>(
                        builder: (ctx) => MethodChannelPage(
                            "MethodChannel 实现 Flutter 与 原生双向通信")));
              },
              child: const Text(
                "MethodChannel 通信",
              )),
        ),
        Padding(
          padding: const EdgeInsets.all(20),
          child: TextButton(
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                    const BorderSide(color: Colors.grey, width: 1)),
              ),
              onPressed: () {
                Navigator.push(
                    context,
                    CupertinoPageRoute<void>(
                        builder: (ctx) => EventChannelPage(
                            "EventChannel 实现 原生向Flutter 发送消息")));
              },
              child: const Text(
                "EventChannel 通信",
              )),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    String title = "";
    if (Platform.isIOS) {
      //ios相关代码
      title = "flutter 与 Ios 双向互调 ";
    } else if (Platform.isAndroid) {
      //android相关代码
      title = "flutter 与 Android 双向互调";
    }

    return GetBuilder<PrimitiveController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.blue,
            //标题居中显示
            centerTitle: true,
            //返回按钮占位
            leading: Container(),
            //标题显示
            title: Text(title),
          ),
          body: SafeArea(
            child: _buildView(context),
          ),
        );
      },
    );
  }
}
